package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.InstructionsFragment;
import com.af.monitorModule.generator.mapper.InstructionsFragmentMapper;
import com.af.monitorModule.generator.service.InstructionsFragmentService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-11-18
 */
@Service
public class InstructionsFragmentServiceImpl extends ServiceImpl<InstructionsFragmentMapper, InstructionsFragment> implements InstructionsFragmentService {

    @Autowired
    private InstructionsFragmentMapper instructionsFragmentMapper;

    @Override
    public JSONObject createFragmentTask(InstructionsFragment instructionsFragment) {
        QueryWrapper<InstructionsFragment> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",instructionsFragment.getCompanyId())
                .eq("db_name",instructionsFragment.getDbName())
                .eq("table_name",instructionsFragment.getTableName())
                .eq("index_name",instructionsFragment.getIndexName())
        .and(qw -> qw.eq("task_state",0).or().eq("task_state",1));
        List<InstructionsFragment> instructionsFragments = instructionsFragmentMapper.selectList(wrapper);
        if (instructionsFragments.size() > 0){
            return ResultEnum.TASK_IS_REPEAT.getResult();
        }
        instructionsFragmentMapper.insert(instructionsFragment.setInputDate(LocalDateTime.now()).setTaskState(0));
        return ResultEnum.OK.getResult();
    }
}
